Package earth

Source Code of earth.GEServer$GEHttpHandler

package earth;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Formatter;
import java.util.Locale;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import core.Parachute;
import core.Plane;

public class GEServer {
 
  private Plane plane;
  private Parachute parachute;
 
  public GEServer(Plane plane, Parachute parachute) {
    this.plane = plane;
    this.parachute = parachute;
   
  }
 
  public void startServer() {
        InetSocketAddress addr = new InetSocketAddress(8080);
        HttpServer server;
    try {
      server = HttpServer.create(addr, 0);
          server.createContext("/", new GEHttpHandler());
          server.setExecutor(Executors.newCachedThreadPool());
          server.start();
         
          BufferedReader reader = new BufferedReader(new FileReader("kml.xml"));
          StringBuilder sb = new StringBuilder();
          String line = null;
          while((line = reader.readLine()) != null) {
            sb.append(line);
          }
          reader.close();
          kml = sb.toString();
          System.out.println("Server is listening on port 8080");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
 
    private class GEHttpHandler implements HttpHandler {
        public void handle(HttpExchange exchange) throws IOException {
            String requestMethod = exchange.getRequestMethod();
            if (requestMethod.equalsIgnoreCase("GET")) {
                Headers responseHeaders = exchange.getResponseHeaders();
                responseHeaders.set("Content-Type", "application/vnd.google-earth.kml+xml .kml");
                exchange.sendResponseHeaders(200, 0);

                OutputStream responseBody = exchange.getResponseBody();
                StringBuilder sb = new StringBuilder();
                Formatter formatter = new Formatter(sb, Locale.US);
                formatter.format(kml,
                    plane.getLontitude(), plane.getLatitude(), plane.getAltitude(),
                    parachute.getLongitude(), parachute.getLatitude(), parachute.getAltitude());
                responseBody.write(sb.toString().getBytes());
                responseBody.close();
                System.out.println("sent: " + sb.toString());
            }
        }
    }

    String kml;
}

TOP

Related Classes of earth.GEServer$GEHttpHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.